﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Windows.Forms;

namespace RFID_code_demo
{
    public partial class Form1 : Form
    {

        Client c_Ipch = new Client();
        private bool bIPCH = false;
        private delegate void de_OutputMessage(string str);
        string[] strArray = new string[10];
        bool connected = false;

        public Form1()
        {
            InitializeComponent();
            bIPCH = false;
            c_Ipch.SysHintEvent += new EventHandler<EventArgs>(recive_Ipch);
        }

        public bool wait_response()
        {
            for (int a = 1; a < 500; a = a + 1)
            {
                Thread.Sleep(1);
                if (strArray[0] == "sAN") { break; }
                if (a == 499)
                {
                    MessageBox.Show("无命令反馈");
                    return false;
                }
            }
            return true;
        }
        public bool connect_Ipch()
        {
            bool bresult = false;
            if (!bIPCH)
            {
                bresult = c_Ipch.ConnectServer(ip_box.Text.Trim(), Int32.Parse(port_box.Text.Trim()));
                if (bresult)
                {
                    bIPCH = true;
                }
            }
            return bresult;
        }

        public bool disconnect_Ipch()
        {
            bool bresult = false;
            bresult = c_Ipch.Disconnect();
            bIPCH = false;
            return bresult;
        }

        public bool Send_To_IpchBybyte(byte[] bytebuff)
        {
            bool bresult = false;
            byte[] byteSend = new byte[bytebuff.Length + 2];
            byteSend[0] = 0x02;
            for (int i = 0; i < bytebuff.Length; i++)
            {
                byteSend[i + 1] = bytebuff[i];
            }
            byteSend[byteSend.Length - 1] = 0x03;
            bresult = c_Ipch.BeginSend(byteSend);
            return bresult;
        }

        private void recive_Ipch(object sender, EventArgs e)
        {
            string dataValue = sender as string;
            dataValue = dataValue.Replace("\0", "");
            dataValue = DeleteUnVisibleChar(dataValue);
            strArray = dataValue.Split(' ');
        }

        public static string DeleteUnVisibleChar(string sourceString)
        {
            System.Text.StringBuilder sBuilder = new System.Text.StringBuilder(131);
            for (int i = 0; i < sourceString.Length; i++)
            {
                int Unicode = sourceString[i];
                if (Unicode >= 16)
                {
                    sBuilder.Append(sourceString[i].ToString());
                }
            }
            return sBuilder.ToString();
        }

        private void get_UII_space()
        {
            strArray[0] = null;
            string command = "sMN TEPrbUnTr 0 1";
            Send_To_IpchBybyte(Encoding.Default.GetBytes(command));
            bool pass = wait_response();
            if (pass == false) { }
            else
            {
                uii_size_box.Text = Convert.ToString(Convert.ToInt32(strArray[2], 16));
            }
        }
        /// <summary>
        ///第三位为偏移量 0 CRC 1 PC 
        /// </summary>
        private void get_UII()
        {
            strArray[0] = null;
            string command = "";
            command = "sMN TAreadTagData 0 1 2 " + Convert.ToString(Convert.ToInt32(uii_size_box.Text) - 2) + " 32";
            Send_To_IpchBybyte(Encoding.Default.GetBytes(command));
            bool pass = wait_response();
            if (pass == false) { }
            else
            {
                uii_box.Text = strArray[4];
            }
        }
        /// <summary>
        /// 第三个0 为偏移量
        /// </summary>
        private void get_TID()
        {
            strArray[0] = null;
            string command = "";
            command = "sMN TAreadTagData 0 2 0 " + Convert.ToString(Convert.ToInt32(uii_size_box.Text) - 2) + " 32";
            Send_To_IpchBybyte(Encoding.Default.GetBytes(command));
            bool pass = wait_response();
            if (pass == false) { }
            else
            {
                tid_box.Text = strArray[4];
            }
        }
        private void get_User()
        {
            strArray[0] = null;
            string command = "";
            //  command = "sMN TAreadTagData 28 3 0 20 32";
            //command = "sMN TAreadTagData 0 3 0 " + "20" + " 32";
            command = "sMN TAreadTagData 0 3 0 20 32";
            Send_To_IpchBybyte(Encoding.Default.GetBytes(command));
            bool pass = wait_response();
            if (pass == false) { }
            else
            {
               // tid_box.Text = strArray[4];

                byte[] byteArray = Enumerable.Range(0, strArray[4].Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(strArray[4].Substring(x, 2), 16))
                             .ToArray();
                string resultString = Encoding.Default.GetString(byteArray);
                textBox1.Text = resultString;
            }
        }




        private void connect_button_Click(object sender, EventArgs e)
        {
            if (connected == false)
            {
                if (connect_Ipch())
                {
                    connected = true;
                    port_box.Enabled = false;
                    ip_box.Enabled = false;
                    detect_button.Enabled = true;
                    connect_button.Text = "断开连接";
                    MessageBox.Show("连接成功");
                    Console.WriteLine("连接成功");
                }
                else
                {
                    MessageBox.Show("连接失败");
                }
            }
            else
            {
                if (disconnect_Ipch())
                {
                    connected = false;
                    port_box.Enabled = true;
                    ip_box.Enabled = true;
                    detect_button.Enabled = false;
                    connect_button.Text = "建立连接";
                    MessageBox.Show("断开连接成功");
                    Console.WriteLine("断开成功");
                }
                else
                {
                    MessageBox.Show("断开连接失败");
                }
            }
        }

        private void detect_button_Click(object sender, EventArgs e)
        {
            get_UII_space();
            get_UII();
       //     get_TID();
          //  get_User();
        }

        public int index = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            Task.Run(() =>
            {
                while (index > 10)
                {

                    Thread.Sleep(1000);

                }
            });
        }

        private void button2_Click(object sender, EventArgs e)
        {
            index = 20;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            index = 5;
        }
    }
}
